package com.klikmee.sms.core.base.communicator.mts;

import java.text.MessageFormat;
import java.util.concurrent.ConcurrentMap;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.base.CoreCacheDriver;
import com.klikmee.sms.core.base.communicator.DeliveryReport;
import com.klikmee.sms.core.base.util.Constants.Loggers;
import com.klikmee.sms.core.model.OutgoingMessage;

/**
 * Mts implementation of cache driver
 * 
 * @author makcro
 * 
 */
public class MtsChargedMessageCacheDriverImpl implements CoreCacheDriver<DeliveryReport, OutgoingMessage> {

	/**
	 * Logger instance
	 */
	private Logger									logger	= Logger.getLogger( Loggers.CORE_LOGGER );

	/**
	 * Charged cache reference
	 */
	private ConcurrentMap<String, OutgoingMessage>	chargedCache;

	public MtsChargedMessageCacheDriverImpl() {
	}

	/**
	 * writes charged message to cache using {@link OutgoingMessage#getId()}} as key
	 */
	@Override
	public void write(OutgoingMessage object) {
		chargedCache.put( object.getId().toString(), object );
		logger.info( MessageFormat.format( "Outgoing message with id [{0}] written to cache", object.getId() ) );
	}

	/**
	 * read charged message to cache using {@link DeliveryReport#getMessageId()} as key
	 */
	@Override
	public OutgoingMessage read(DeliveryReport key) {
		OutgoingMessage outgoingMessage = chargedCache.get( key.getMessageId().toString() );
		if (outgoingMessage != null) {
			logger.debug( MessageFormat.format( "Outgoing message with id [{0}] read from cache", outgoingMessage.getId() ) );
		}
		return outgoingMessage;
	}

	/**
	 * remove charged message from cache using {@link OutgoingMessage#getId()}} as key
	 */
	@Override
	public void remove(OutgoingMessage object) {
		OutgoingMessage outgoingMessage = chargedCache.remove( object.getId().toString());
		logger.debug( MessageFormat.format( "Outgoing message with id [{0}] removed from cache", outgoingMessage != null ? outgoingMessage.getId() : "Message is NULL" ) );
	}
	
	public ConcurrentMap<String, OutgoingMessage> getChargedCache() {
		return chargedCache;
	}
	
	public void setChargedCache(ConcurrentMap<String, OutgoingMessage> chargedCache) {
		this.chargedCache = chargedCache;
	}

}
